<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        屏保时间: {{ screenTime.minute }} 分钟
      </el-col>
      <el-col :span="1.5">
        <el-switch
          v-model="screenTime.showed"
          active-text="开启屏保"
          inactive-text="关闭屏保"
          disabled
          >
        </el-switch>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleChangeTime"
        >设置</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="页面号" align="center" prop="id" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleGet(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >替换</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <el-dialog :visible.sync="uploadOpen" width="500px" :destroy-on-close="true">
      <!-- 下拉框 -->
      <el-select v-model="id" disabled>
        <el-option v-for="item in screens"
        :label="item + '号页面'"
        :value="item"
        :key="item"
        >
        </el-option>
      </el-select>
      <common-upload style="margin: 20px 0;" type="image" :url="fileUploadUrl + id" width="400px" />
      <!-- <p>温馨提示，上传图片的分辨率应为 1080 * 1920</p> -->
  </el-dialog>
  <el-dialog :visible.sync="changeTimeOpen" width="500px" append-to-body>
    <el-form ref="form" label-width="180px">
      <el-form-item label="屏保时间(单位：分钟)">
          <el-input-number v-model="newScreenTime" :min="1" :max="999"></el-input-number>
        </el-form-item>
        <el-form-item label="屏保设置选项">
          <el-switch
          v-model="screenShow"
          active-text="开启屏保"
          inactive-text="关闭屏保"
          >
        </el-switch>
        </el-form-item>
    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTime">确 定</el-button>
        <el-button @click="cancelTime">取 消</el-button>
      </div>
    </el-dialog>

  <!-- <el-dialog :visible.sync="changeTimeOpen" width="500px">

  </el-dialog> -->
  </div>

</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, getScreenTime, setScreenTime } from "@/api/resource/info";
import CommonUpload from '@/components/CommonUpload/index.vue';
import axios from 'axios';
import { SCREENSAVER_UPLOAD_URL } from '@/utils/constant.js';
export default {
  name: "Screensaver",
  components: {
    CommonUpload
  },
  data() {
    return {
      //文件上传框打开
      uploadOpen: false,
      // 上传文件类型,
      uploadType: 'video',
      uploadTypes: [{label: 'pdf', value: 'pdf'}, {label: 'video', value: 'video'}, {label: 'i', value: 'img'}],
      //
      fileUploadUrl: SCREENSAVER_UPLOAD_URL,
      // 遮罩层
      loading: false,
      // 屏幕数组
      screens: [1, 2, 3],
      // 欲操作的屏幕id
      id: 1,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 屏保时间
      screenTime: { minute: 20, showed: true },
      screenShow: true,
      // dialog界面屏保时间
      newScreenTime: 0,
      changeTimeOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        screenNumber: null,
        hasResource: null,
        resourceType: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      // this.loading = true;
      // let promises = [
      //   axios('http://116.204.109.57:8081/screensave/1'),
      //   axios('http://116.204.109.57:8081/screensave/2'),
      //   axios('http://116.204.109.57:8081/screensave/3')
      // ]
      // Promise.allSettled(promises).then(res => {
      //   console.log(res);
      //   this.total = 3;
      //   this.loading = false;
      // }).catch(err => {
      //   this.loading = false;
      // })
      this.infoList = Array.of(1, 2, 3).map(val => {
        return { "id": val }
      })
      this.total = 3;
      // listInfo(this.queryParams).then(response => {
      //   this.infoList = response.rows;
      //   this.total = 3;
      //   this.loading = false;
      // }).catch(err => {
      //   this.loading = false;
      // });
      getScreenTime().then(res => {
        this.screenTime = res.data
        this.screenShow = res.data.showed
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        screenNumber: null,
        hasResource: null,
        resourceType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改屏保时间 */
    handleChangeTime() {
      this.changeTimeOpen = true
      this.newScreenTime = this.screenTime.minute
      this.screenShow = this.screenTime.showed
    },
    cancelTime() {
      this.changeTimeOpen = false;
    },
    submitTime() {
      setScreenTime(this.newScreenTime, this.screenShow).then(res => {
        if (res.code === 200) {
          this.$message.success('设置屏保时间成功!');
        }
        this.changeTimeOpen = false;
        this.getList();
      })
    },
    /** 查看屏保 */
    handleGet(row) {
      const { id } = row
      window.open(`${SCREENSAVER_UPLOAD_URL}${id}`, '_blank')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const { id } = row;
      this.id = id;
      this.uploadOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
